<?php

namespace App\Http\Controllers\Shop;

use App\Model\AddType;
use App\Model\AddShopType;
use App\Model\ShopTypeModel;
use App\Model\AdminLogsModel;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class TypeController extends Controller
{

	//类型列表
	public function index(Request $request)
	{
		//搜索条件
		$keywords = $request->input('keywords');//关键字
		$pid = $request->input('pid');
		$pageParam['keywords'] = $keywords;
		$pageParam['pid'] = $pid;

		//类别(搜索)
		$sql = "SELECT * FROM `add_type` ORDER BY concat(path,id),sort desc";
		$all_list = DB::select($sql);

		//类别列表(展示)
		$order = "SELECT * FROM `add_type` ORDER BY concat(path,id),sort desc";
		$list = DB::table(DB::raw("($order) as res"))->where(function ($query) {
			if (request()->keywords) {
				$query->where('name','like',"%".request()->keywords."%");
			}
			if(request()->pid){
				$query->where('id',request()->pid)
					->Orwhere('path','like',"%".request()->pid."%");
			}
		})->paginate(15);
		foreach($list->items() as $k=>$v){
			$v->exit = 2;
			if(AddType::where('path','like',"%,".$v->id.",%")->first()){
				$v->exit = 1;
			}
		}

		$page = $request->input('page','');
		return view('shop.type.index')
			->with('keywords',$keywords)
			->with('page',$page)
			->with('pid',$pid)
			->with('request',$request)
			->with('all_list',$all_list)
			->with('list',$list);
	}

	//添加页
	public function add()
	{
		return view('shop.type.add');
	}

	//添加
	public function insert(){
//		if(AddType::where('name',request()->name)->first()){
//			return ['code'=>0,'msg'=>'该类别名称已存在'];
//		}
		//事物
		DB::beginTransaction();
		try {
			$type = New AddType();
			$type->pid = 0;
			$type->path = '0,';
			$type->level = 0;
			if(AddType::where('name',request()->name)->first()){
				return ['code'=>0,'msg'=>'该类别名称已存在'];
			}
			if(request()->pid){
				if(AddType::where('name',request()->name)->where('pid',request()->pid)->first()){
					return ['code'=>0,'msg'=>'该类别名称已存在'];
				}
				$ptype = AddType::where('id',request()->pid)->first();
				if($ptype){
					$type->pid = $ptype->id;
					$type->path = $ptype->path.$ptype->id.",";
					$type->level = $ptype->level+1;
				}else{
					return returnJson(0, '该父类不存在');
				}
			}
			$type->name = request()->name;
			$type->sort = 0;
			$type->save();

			$msg = '【货源后台】添加类别:'.request()->name.'(id:'.$type['id'].')';
			AdminLogsModel::addLogs($msg);
			DB::commit();
			return ['code'=>1,'msg'=>'添加成功'];
		} catch (Exception $e) {
			DB::rollBack();
			throw $e;
			return returnJson(0,'操作失败,请稍后再试');
		}
	}

	//添加子类
	public function add_son(){
		$id = request()->id;
		$fname = AddType::where('id',$id)->value('name');
		return view('shop.type.add_son',compact('id','fname'));
	}


	//删除
	public function del()
	{
		DB::beginTransaction();
		try{
			$id = request()->id;
			$list = AddType::where('id',$id)->first();
			if(AddShopType::where('tid',$id)->first()){
				return returnJson(0, '该品牌分类下存在商品,不可删除');
			}else{
				if(AddType::where('pid',$id)->first()){
					return returnJson(0, '该分类下存在子分类,不可删除');
				}
				AddType::where('id',$id)->delete();
				ShopTypeModel::where('tid',$id)->delete();
			}
			$msg = '【货源后台】删除分类:'.$list['name'].'(id:'.$id.')';
			AdminLogsModel::addLogs($msg);
			DB::commit();
			return ['code'=>1,'msg'=>'删除成功'];
		}catch(Exception $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>'删除失败'];
		}


	}

	//编辑
	public function edit(){
		$id = request()->id;
		$list = AddType::where('id',$id)->first();
		return view('shop.type.edit',compact('id','list'));
	}

	//编辑
	public function update(){
		DB::beginTransaction();
		try{
			$list = AddType::where('id', request()->id)->first();
			if($list['pid'] >0){
				$list = AddType::where('pid', $list['pid'])->where('name',request()->name)->first();
				if($list){
					return ['code'=>0,'msg'=>'该类别名称已存在'];
				}
			}
			if(AddType::where('name',request()->name)->where('pid',0)->where('id','<>',request()->id)->first()){
				return ['code'=>0,'msg'=>'该类别名称已存在'];
			}
			AddType::where('id', request()->id)->update([
				"name" => request()->name,
				"sort"=>0,
			]);
			if($list['state'] == 1){
				ShopTypeModel::where('tid',request()->id)->update([
					'name' => request()->name,
					'sort' =>0,
				]);
			}
			$msg = '【货源后台】编辑分类:'.$list['name'].'(id:'.request()->id.')';
			AdminLogsModel::addLogs($msg);
			DB::commit();
		}catch(Exception $e){
			DB::rollBack();
			return returnJson(0, '更改失败');
		}
		return returnJson(1, '更改成功');



		$list = AddType::where('id', request()->id)->first();
		if($list['pid'] >0){
			$list = AddType::where('pid', $list['pid'])->where('name',request()->name)->first();
			if($list){
				return ['code'=>0,'msg'=>'该类别名称已存在'];
			}
		}
		if(AddType::where('name',request()->name)->where('pid',0)->where('id','<>',request()->id)->first()){
			return ['code'=>0,'msg'=>'该类别名称已存在'];
		}
		if (AddType::where('id', request()->id)->update([
			"name" => request()->name,
			"sort"=>0,
		])) {
			return returnJson(1, '更改成功');
		} else {
			return returnJson(0, '更改失败');
		}
	}

	//同步
	public function synchro(){
		$list = AddType::where('state',2)->get();
		if(count($list) <= 0){
			return ['code'=>0,'msg'=>'没有需要同步的数据!'];
		}

		DB::beginTransaction();
		try{
			foreach($list as $k=>$v){
				ShopTypeModel::create([
					'name' => $v['name'],
					'pid' => $v['pid'],
					'path' => $v['path'],
					'level' => $v['level'],
					'tid' => $v['id'],
				]);
				AddType::where('id',$v['id'])->update(['state'=>1]);
			}
			DB::commit();
			$msg = '【货源后台】同步分类';
			AdminLogsModel::addLogs($msg);
			return ['code'=>1,'msg'=>'已同步'];
		}catch(Exception $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>'同步失败'];
		}
	}

	//是否在首页显示
	public function display(){
		$id = request()->id;
		$state = request()->state;
		DB::beginTransaction();
		try{
			$list = AddType::where('id',$id)->first();
			AddType::where('id',$id)->update(['display'=>$state]);
			if($list['state'] == 1){
				ShopTypeModel::where('tid',request()->id)->update([
					'display' => $state,
				]);
			}
			DB::commit();
		}catch(Exception $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>'设置失败'];
		}
		return ['code'=>1,'msg'=>'设置成功'];
	}

	//分类排序
	public function update_sort(){
		$id = request()->id;
		$sort = request()->num;
		DB::beginTransaction();
		try{
			$list = AddType::where('id',$id)->first();
			AddType::where('id',$id)->update(['sort'=>$sort]);

			if($list['state'] == 1){
				ShopTypeModel::where('tid',request()->id)->update([
					'sort' => $sort,
				]);
			}
			DB::commit();
		}catch(Exception $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>'设置失败'];
		}
		return ['code'=>1,'msg'=>'设置成功'];
	}


}
